package com.algrithom.math;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Queue;

public class Solution1 {
    
    private final List<Integer> list = new ArrayList<>();
    
    private final Queue<Integer> maxQueue = new PriorityQueue<>(Comparator.reverseOrder());
    
    private final Queue<Integer> minQueue = new PriorityQueue<>();
    
    public static void main(String[] args){
        Solution1 solution1 = new Solution1();
        int[] array = new int[]{4,7,1,3,2,10};
        for (int ar : array) {
            solution1.addNum(ar);
        }
        System.out.println(solution1.findMedian());
    }
    
    public void addNum(int num){
        if (maxQueue.size() == 0 || maxQueue.peek() > num) {
            maxQueue.offer(num);
        } else {
            minQueue.offer(num);
        }
        if (maxQueue.size() > minQueue.size() + 1) {
            minQueue.add(maxQueue.poll());
        } else if (maxQueue.size() < minQueue.size()) {
            maxQueue.add(minQueue.poll());
        }
    }
    
    public double findMedian(){
        double mediea = 0;
        if (maxQueue.size() == minQueue.size()) {
            mediea = (maxQueue.peek() + minQueue.peek()) / 2.0;
        } else {
            mediea = maxQueue.peek();
        }
        return mediea;
    }
}
